# coding: utf-8

# Models
from django.db import models
from django.contrib.auth.models import User

# Fields
from filebrowser.fields import FileBrowseField

# Settings
from settings import *

# n'Stuff ;)
from django.utils.translation import ugettext as _

import datetime

class FileData(models.Model):
   """simple file model"""
   filename = models.CharField("Filename", max_length=255)
   
   description = models.TextField("Description", null=True, blank=True)
   
   size = models.IntegerField("Size (kb)")
   
   create_date = models.DateTimeField('date created')
   modify_date = models.DateTimeField('date modified', null=True, blank=True)
   
   user = models.ForeignKey(User)
   
   #filefield
   
   def __unicode__(self):
      """docstring for __unicode__"""
      return self.filename


class FolderData(FileData):
    """
    NOTE: check how to implement
    """
    pass


class AudioData(FileData):
    
    file_field = FileBrowseField("Audio", extensions=AUDIO_TYPES, max_length=200, blank=True, null=True)
    
    length = models.FloatField(null=True, blank=True)
    bitrate = models.IntegerField(null=True, blank=True)
    
    id3_artist = models.CharField(max_length=255, null=True, blank=True)
    id3_album = models.CharField(max_length=255, null=True, blank=True)
    id3_track = models.CharField(max_length=255, null=True, blank=True)


class ImageData(FileData):
    """
    ImageData == metadata of an image file.
    metadata == information about the image gathered via pil and exif.py
    
    What metadata is saved?
    Pretty much all of it ;)
    No seriously, its loosely based on what flickr shows when you
    click "More properties" at "Additional Information" in the sidebar.
    """
    
    file_field = FileBrowseField("Image", extensions=IMAGE_TYPES, max_length=200, blank=True, null=True)
    
    ### gathered via pil
    
    # integers
    width = models.IntegerField("Width", null=True, blank=True)
    height = models.IntegerField("Height", null=True, blank=True)
    
    ### gathered via exif.py
    
    # dateTime
    dateTimeDigitized = models.DateTimeField("Date and Time (Digitized)", null=True, blank=True)
    dateTimeOriginal = models.DateTimeField("Date and Time (Original)", null=True, blank=True)
    
    # char
    exifVersion = models.CharField("EXIF Version", max_length=20, null=True, blank=True)
    exposureTime = models.CharField("Exposure", max_length=20, null=True, blank=True)
    fNumber = models.CharField("fNumber", max_length=20, null=True, blank=True)
    flash = models.CharField("Flash", max_length=20, null=True, blank=True)
    focalLength = models.CharField("Focal Length", max_length=20, null=True, blank=True)
    isoSpeedRatings = models.CharField("ISO Speed", max_length=20, null=True, blank=True)
    apertureValue = models.CharField("Aperture", max_length=20, null=True, blank=True)
    maxApertureValue = models.CharField("Max Aperture Value", max_length=20, null=True, blank=True)
    exposureProgram = models.CharField("Exposure Program", max_length=20, null=True, blank=True)
    exposureMode = models.CharField("Exposure Mode", max_length=20, null=True, blank=True)
    shutterSpeedValue = models.CharField("Shutter Speed", max_length=20, null=True, blank=True)
    meteringMode = models.CharField("Metering Mode", max_length=50, null=True, blank=True)
    colorSpace = models.CharField("Color Space", max_length=20, null=True, blank=True)
    compressedBitsPerPixel = models.CharField("Compressed Bits Per Pixel", max_length=20, null=True, blank=True)
    lightSource = models.CharField("Light Source", max_length=20, null=True, blank=True)
    subSecTimeOriginal = models.CharField("Sub Sec Time Original", max_length=20, null=True, blank=True)
    subSecTimeDigitized = models.CharField("Sub Sec Time Digitized", max_length=20, null=True, blank=True)
    sensingMethod = models.CharField("Sensing Method", max_length=20, null=True, blank=True)
    customRendered = models.CharField("Custom Rendered", max_length=20, null=True, blank=True)
    whiteBalance = models.CharField("White Balance", max_length=20, null=True, blank=True)
    digitalZoomRatio = models.CharField("Digital Zoom Ratio", max_length=20, null=True, blank=True)
    sceneCaptureType = models.CharField("Scene Capture Type", max_length=20, null=True, blank=True)
    subjectDistanceRange = models.CharField("Subject Distance Range", max_length=20, null=True, blank=True)
    contrast = models.CharField("Contrast", max_length=20, null=True, blank=True)
    saturation = models.CharField("Saturation", max_length=20, null=True, blank=True)
    sharpness = models.CharField("Sharpness", max_length=20, null=True, blank=True)
    exifOffset = models.CharField("Sharpness", max_length=20, null=True, blank=True)
    orientation = models.CharField("Orientation", max_length=20, null=True, blank=True)
    resolutionUnit = models.CharField("Resolution Unit", max_length=20, null=True, blank=True)
    xResolution = models.CharField("X-Resolution", max_length=20, null=True, blank=True)
    yResolution = models.CharField("Y-Resolution", max_length=20, null=True, blank=True)
    model = models.CharField("Camera Model", max_length=20, null=True, blank=True)
    make = models.CharField("Camera Manufacturer", max_length=20, null=True, blank=True)
    software = models.CharField("Software", max_length=255, null=True, blank=True)
    YCbCrPositioning = models.CharField("YCbCr Positioning", max_length=20, null=True, blank=True)
    gpsLongitude = models.CharField("GPS Longitude", max_length=20, null=True, blank=True)
    gpsLongitudeRef = models.CharField("GPS Longitude Ref", max_length=20, null=True, blank=True)
    gpsLatitude = models.CharField("GPS Latitude", max_length=20, null=True, blank=True)
    gpsLatitudeRef = models.CharField("GPS Latitude Ref", max_length=20, null=True, blank=True)
    gpsImgDirection = models.CharField("GPS Imgage Direction", max_length=20, null=True, blank=True)
    gpsImgDirectionRef = models.CharField("GPS Imgage Direction Ref", max_length=20, null=True, blank=True)
    gpsTimeStamp = models.CharField("GPS TimeStamp", max_length=20, null=True, blank=True)
    
    def get_thumb_img_tag(self):
        from filebrowser.settings import ADMIN_THUMBNAIL, MEDIA_ROOT
        from filebrowser.functions import version_generator, get_version_path, url_to_path, path_to_url
        import os
        
        source = self.file_field.path
        version_prefix = ADMIN_THUMBNAIL
        
        version_path = get_version_path(url_to_path(str(source)), version_prefix)
        if not os.path.isfile(os.path.join(MEDIA_ROOT, version_path)):
            # create version
            version_path = version_generator(url_to_path(str(source)), version_prefix)
        elif os.path.getmtime(os.path.join(MEDIA_ROOT, url_to_path(str(source)))) > os.path.getmtime(os.path.join(MEDIA_ROOT, version_path)):
            # recreate version if original image was updated
            version_path = version_generator(url_to_path(str(source)), version_prefix, force=True)
        return '<img src="' + path_to_url(version_path) + '" />'
    get_thumb_img_tag.allow_tags = True


class VideoData(FileData):
    
    file_field = FileBrowseField("Video", extensions=VIDEO_TYPES, max_length=200, blank=True, null=True)
    
    width = models.IntegerField(null=True, blank=True)
    height = models.IntegerField(null=True, blank=True)
    
    length = models.FloatField(null=True, blank=True)
    
    video_format = models.CharField(max_length=20, null=True, blank=True)
    video_bitrate = models.IntegerField(null=True, blank=True)
    fps = models.FloatField(null=True, blank=True)
    
    audio_format = models.CharField(max_length=20, null=True, blank=True)
    audio_bitrate = models.IntegerField(null=True, blank=True)
    
    